<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(5)//每页条数

const foodName = ref('')
const foodType = ref('')

const food_info = ref([
    {
        "name":"水煮肉片",
        "type":"荤菜",
        "stats":"江湖菜"
    },
    {
        "name":"水煮白菜",
        "type":"素菜",
        "stats":"蔬菜"
    }
])


//声明一个异步函数，显示食谱信息
import {ElMessage,ElMessageBox} from 'element-plus'
import {foodInfoService} from '@/api/cook.js'
const foodList = async()=>{
    let params = {
        pageNum:pageNum.value,
        pageSize:pageSize.value,
        foodName:foodName.value?foodName.value:null,
        foodType:foodType.value?foodType.value:null
    }
    console.log(params)
    let result = await foodInfoService(params);
    food_info.value = result.data;

    total.value = result.data.total;
    food_info.value = result.data.items;

}
foodList();


// const deleteOne = async(row)=>{
//     ElMessageBox.confirm(
//         '您确定要删除该用户吗？',
//         '温馨提示',
//         {
//             confirmButtonText: '确认',
//             cancelButtonText: '取消',
//             type: 'warning',
//         }
//     )
//     .then(async () => {
//         let result = await deleteOneService(row);
//         ElMessage.success(result.msg?result.msg:'成功删除用户');
//         userList();
//     })
//     .catch(() => {
//         ElMessage({
//             type: 'info',
//             message: '取消删除用户',
//         })
//     })
// }

//控制添加分类弹窗
const dialogVisible = ref(false)

//添加分类表单校验
const rules = {
    id: [
        { required: true, message: '请输入用户账号', trigger: 'blur' },
    ]
}


//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    userList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
}

</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>用户列表</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible=true,title='添加用户'">添加用户</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="菜品类别：">
                <el-select placeholder="请选择" v-model="foodType" style="width:200px">
                    <el-option v-for="item in food_info"
                    :key="item.stats"
                    :label="item.label"
                    :value="item.stats"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="具体搜索：" >
                <el-input placeholder="请输入要查找的菜品" v-model="foodName" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="foodList">搜索</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="food_info" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="菜品名称" prop="name"></el-table-column>
            <el-table-column label="菜品类别" prop="type"></el-table-column>
            <el-table-column label="菜品属性" prop="stats"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="init(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteOne(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">

        </el-dialog>

        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
        layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
        @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>